package com.mcflies.ui.listView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mcflies.ui.R;
import com.mcflies.ui.listView.AsyncImageLoader.ImageCallBack;

public class AsyncListViewAdapter extends ArrayAdapter<AsyncBean> {
	private AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
	private Map<Integer, View> viewMap = new HashMap<Integer, View>();

	public AsyncListViewAdapter(Activity activity, List<AsyncBean> list) {
		super(activity, 0, list);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = this.viewMap.get(position);
		if (null == rowView) {
			LayoutInflater inflater = ((Activity) this.getContext())
					.getLayoutInflater();
			rowView = inflater.inflate(R.layout.async_listview_item, null);

			AsyncBean asyncBean = this.getItem(position);

			TextView textView = (TextView) rowView
					.findViewById(R.id.asyncListViewItem_TV);
			textView.setText(asyncBean.getTitle());

			final ImageView imageView = (ImageView) rowView
					.findViewById(R.id.asyncListViewItem_IV);
			asyncImageLoader.loadDrawable(asyncBean.getImage(),
					new ImageCallBack() {
						@Override
						public void imageLoad(Drawable drawable, String imageUrl) {
							imageView.setImageDrawable(drawable);
						}
					});

			viewMap.put(position, rowView);
		}
		return rowView;
	}
}
